﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web;
using System.Net.Mail;

namespace SuZhouHouses.Utils
{
    public class MailManager
    {
        public MailManager()
        {
            From = "g.kho2000@gmail.com";
            To = new List<string>();
            //To = "g.kho2000@gmail.com";

            //From = "song.guo@live.com";
            //To = "song.guo@live.com";
        }

        private string From { get; set; }
        public List<string> To { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }

        public void Send()
        {
            if (To == null || To.Count == 0)
                return;
            
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(From, "发件人");
            foreach (var addr in To)
            {
                if (SuZhouHouses.Utils.Validator.IsValidEmail(addr))
                    mailMessage.To.Add(new MailAddress(addr, "收件人"));
            }
            // no valid email
            if (mailMessage.To.Count == 0)
                return;

            //mailMessage.Bcc.Add(new MailAddress("BCCPerson@sina.com", "密送人"));
            //mailMessage.CC.Add(new MailAddress("CCPerson@tom.com", "抄送人"));
            mailMessage.Subject = Subject;
            mailMessage.Body = Body;
            //Attachment attachement = new Attachment(@"C:/test.txt");
            //mailMessage.Attachments.Add(attachement);
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.EnableSsl = true;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Credentials = new System.Net.NetworkCredential("g.kho2000@gmail.com", "in5pur2012");
            //smtpClient.Credentials = new System.Net.NetworkCredential("song.guo@live.com", "Let'sGO!");
            smtpClient.Send(mailMessage); 
        }
    }
}